﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rem.Objects;
using FileDB;
namespace Rem.Objects.Forms
{
    public partial class GoToForm : Form
    {
        public int SelectedID
        {
            get
            {
                int result;
                if (Int32.TryParse(this.txtID.Text, out result))
                    return result;
                else
                    return QueryConstants.UnsavedObjectId;
            }
        }
        public string SearchByText
        {
            get
            {
                return this.txtID.Text.Trim();
            }
        }
        public GoToForm(BaseObjectTypes baseType, bool readOnlyBaseType)
        {
            InitializeComponent();
            InitializeForm(baseType, readOnlyBaseType);
            //remDatabaseEntities = BaseForm.RemDatabaseEntities;
        }

        private void InitializeForm(BaseObjectTypes baseType, bool readOnlyBaseType)
        {
            remComboBox1.DataSource = (from a in QueryObject.BaseObjectInfo
                                       select new { Display = a.Value, ObjectType = a.Key }).ToList();
            remComboBox1.DisplayMember = "Display";
            remComboBox1.ValueMember = "ObjectType";

            remComboBox1.SelectedText = QueryObject.BaseObjectInfo[baseType];
            remComboBox1.SelectedValue = baseType;

            remComboBox1.Enabled = readOnlyBaseType;
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.Text = "转到" + QueryObject.BaseObjectInfo[baseType];
            this.Label1.Text = string.Format("{0}编号/名字:", QueryObject.BaseObjectInfo[baseType]);
            this.btnOK.Text = "确定";
            this.btnCancel.Text = "取消";
        }

        public static void OpenForm(string IDField, string FieldName, BaseObjectTypes baseType)
        {
            var ids = GoToForm.GetID(IDField, FieldName, baseType);
            int objectId = ids.Count == 1 ? ids[0] : QueryConstants.UnsavedObjectId;
            if (objectId > 0)
            {
                switch (baseType)
                {
                    case BaseObjectTypes.Customer:
                        Customer cc = (from c in RemDatabaseEntities.Customers
                                       where c.Id == objectId
                                       select c
                                             ).SingleOrDefault();
                        if (cc != null && cc.Id > 0)
                        {
                            CustomerForm f = new CustomerForm(cc);
                            f.Show();
                            f.Dock = DockStyle.Fill;

                        }
                        break;
                    case BaseObjectTypes.Employee:
                        Employee ee = (from c in RemDatabaseEntities.Employees
                                       where c.Id == objectId
                                       select c
                                             ).SingleOrDefault();
                        if (ee != null && ee.Id > 0)
                        {
                            EmployeeForm f = new EmployeeForm(ee);
                            f.Show();
                            f.Dock = DockStyle.Fill;
                        }
                        break;
                    case BaseObjectTypes.Contract:
                        Contract tt = (from c in RemDatabaseEntities.Contracts
                                       where c.Id == objectId
                                       select c
                                             ).SingleOrDefault();
                        if (tt != null && tt.Id > 0)
                        {
                            ContractForm f = new ContractForm(tt);
                            f.Show();
                            f.Dock = DockStyle.Fill;
                        }
                        break;
                    case BaseObjectTypes.Reminder:
                        Reminder rr = (from c in RemDatabaseEntities.Reminders
                                       where c.Id == objectId
                                       select c
                                             ).SingleOrDefault();
                        if (rr != null && rr.Id > 0)
                        {
                            ReminderForm2 f = new ReminderForm2(rr);
                            f.Show();
                            f.Dock = DockStyle.Fill;
                        }
                        break;
                }

            }
        }
        public static List<int> GetID(string IDField, string FieldName, BaseObjectTypes baseType)
        {
            List<int> Ids = new List<int>();
            GoToForm gotoForm = new GoToForm(baseType, false);
            IDataObject data = Clipboard.GetDataObject();
            if (data != null && data.GetDataPresent(DataFormats.Text))
            {
                gotoForm.txtID.Text = data.GetData(DataFormats.Text).ToString();
            }

            int Id = QueryConstants.UnsavedObjectId;

            if (gotoForm.ShowDialog() != DialogResult.OK)
            {
                Ids.Add(QueryConstants.UnsavedObjectId);
                return Ids;
            }
            switch (baseType)
            {
                case BaseObjectTypes.Employee:
                    Ids.AddRange((from e in RemDatabaseEntities.Employees
                                  where e.Id == gotoForm.SelectedID
                                  || e.Name.Contains(gotoForm.SearchByText)
                                  select e.Id).ToList());
                    break;
                case BaseObjectTypes.Contract:
                    var contracts = Contract.GetAllContract();
                    Ids.AddRange((from e in contracts
                                  where e.Id == gotoForm.SelectedID
                                  || e.ContractNo.Contains(gotoForm.SearchByText)
                                  || e.Address.Contains(gotoForm.SearchByText)
                                  select e.Id).ToList());
                    break;
                case BaseObjectTypes.Customer:
                    var customers = Customer.GetAllCustomer();
                    Ids.AddRange(
                    (from e in customers
                         where e.Id == gotoForm.SelectedID ||
                         e.Name.Contains(gotoForm.SearchByText)
                         select e.Id).ToList());
                    break;
                case BaseObjectTypes.Reminder:
                    break;
                default:
                    break;
            }

            return Ids;


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void ShowMRUMenuButton_Click(object sender, EventArgs e)
        {

        }
    }
}
